import { defineStore } from 'pinia';
import { ref } from 'vue';
import * as systemApi from '@/api/system';

const useDictStore = defineStore('dict', () => {
  const dicts = ref<any>([]);

  async function getDictList() {
    const response: any = await systemApi.getDictList();
    if (response.code === 20000) {
      dicts.value = response.data.dicts;
    }
  }

  function list(groupCode: string) {
    return dicts.value.filter((item: any) => item.groupCode === groupCode);
  }

  function label(groupCode: string, value: string) {
    return dicts.value.find((item: any) => item.groupCode === groupCode && item.value === value)?.label;
  }

  function color(groupCode: string, value: string) {
    return dicts.value.find((item: any) => item.groupCode === groupCode && item.value === value)?.color;
  }

  function value(groupCode: string, code: string) {
    return dicts.value.find((item: any) => item.groupCode === groupCode && item.code === code)?.value;
  }

  function getDicts(groupCode: string) {
    return dicts.value.filter((item: any) => item.groupCode === groupCode);
  }

  return {
    list,
    value,
    label,
    color,
    getDicts,
    getDictList
  };
});

export default useDictStore;
